package com.stroke.core;

import com.stroke.entity.impl.SampleEntity;
import com.stroke.graphics.ScreenManager;

/**
 * Author: Igor Bubelov
 * Date: 3/3/11 9:30 PM
 */
public class Engine {
    public static Engine instance;

    private Configuration configuration;

    private CoreRunnable updater;
    private CoreRunnable renderer;

    private ScreenManager screenManager;

    public static Engine getInstance() {
        return instance = instance == null ? new Engine() : instance;
    }

    public void initialize(Configuration configuration) {
        this.configuration = configuration;
        this.screenManager = new ScreenManager(configuration);

        EntityManager.getInstance().setScreenManager(screenManager);

        EntityManager.getInstance().addEntity(new SampleEntity(320, 240));

        EntityManager.getInstance().update();
        EntityManager.getInstance().render();

        updater = new Updater(configuration.getUps());
        renderer = new Renderer(configuration.getFps());

        new Thread(updater).start();
        new Thread(renderer).start();

        screenManager.getScreen().setVisible(true);
    }

    /////

    public void switchScreenMode() {

    }

    /////

    public boolean isPaused() {
        return updater.isPaused() && renderer.isPaused();
    }

    public void pause() {
        updater.pause();
        renderer.pause();
    }

    public void resume() {
        updater.resume();
        renderer.resume();
    }

    /////

    public Configuration getConfiguration() {
        return configuration;
    }
}
